package com.sdw.message.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.sdw.message.dao.base.DataBase;
import com.sdw.message.dao.base.DataReader;
import com.sdw.message.pojo.User;
import com.sdw.message.pojo.UserType;

@Repository("userDao")
public class UserDao {

	@Resource
	private DataBase messageDatabase;
	
	public Iterable<User> getAllUser(){
		String sql = "select id, username, password, type, xh, sfzh, email, xm, xb, createtime, modifytime from [dbo].[sys_user]";
		
		DataReader reader = null;
		List<User> list = new ArrayList<User>();
		
		try{
			reader = this.messageDatabase.executeQuery(sql);
			while(reader.next()){
				list.add(this.conver(reader));
			}
			return list;
		}finally{
			if(reader != null){
				reader.close();
			}
		}
	}
	
	public boolean existUserName(String userName){
		String sql = "select id, username, password, type, xh, sfzh, email, xm, xb, createtime, modifytime from [dbo].[sys_user] where username='" + userName + "'";
		
		DataReader reader = null;
		try{
			reader = this.messageDatabase.executeQuery(sql);
			if(reader.next()){
				return true;
			}else{
				return false;
			}
		}finally{
			if(reader != null){
				reader.close();
			}
		}
	}
	
	public boolean validateUserNameAndPassword(String userName, String password){
		String sql = "select id, username, password, type, xh, sfzh, email, xm, xb, createtime, modifytime from [dbo].[sys_user] where username='" + userName + "' and password='" + password + "'";
		
		DataReader reader = null;
		try{
			reader = this.messageDatabase.executeQuery(sql);
			if(reader.next()){
				return true;
			}else{
				return false;
			}
		}finally{
			if(reader != null){
				reader.close();
			}
		}
	}
	
	public User obtainUserByName(String userName){
		String sql = "select id, username, password, type, xh, sfzh, email, xm, xb, createtime, modifytime from [dbo].[sys_user] where username='" + userName + "'";
		
		DataReader reader = null;
		try{
			reader = this.messageDatabase.executeQuery(sql);
			if(reader.next()){
				return this.conver(reader);
			}else{
				return null;
			}
		}finally{
			if(reader != null){
				reader.close();
			}
		}
	}
	
	public User obtainUserByID(long id){
		String sql = "select id, username, password, type, xh, sfzh, email, xm, xb, createtime, modifytime from [dbo].[sys_user] where id=" + id + "";
		
		DataReader reader = null;
		try{
			reader = this.messageDatabase.executeQuery(sql);
			if(reader.next()){
				return this.conver(reader);
			}else{
				return null;
			}
		}finally{
			if(reader != null){
				reader.close();
			}
		}
	}
	
	private User conver(DataReader reader){
		int index = 1;
		User user = new User();
		
		user.setId(reader.getLong(index++));
		user.setUserName(reader.getString(index++));
		user.setPassword(reader.getString(index++));
		user.setType(UserType.parseName(reader.getString(index++)));
		user.setXh(reader.getString(index++));
		user.setSfzh(reader.getString(index++));
		user.setEmail(reader.getString(index++));
		user.setXm(reader.getString(index++));
		user.setXb(reader.getString(index++));
		user.setCreateTime(reader.getDate(index++));
		user.setModifyTime(reader.getDate(index++));
		
		return user;
	}
}
